package myjava.p_classLoader;

/**
 * @Auther: cty
 * @Date: 2020/4/14 19:29
 * @Description:
 * @version: 1.0
 */
/**
 * 线程上下文类加载器的测试
 * @author 尚学堂高淇 www.sxt.cn
 *
 */
public class Demo05 {
    // 获取当前工程路径
    static String projectPath = System.getProperty("user.dir");
    // 指定文件输出路径
    static String path = "/Users/cty/mycode";

    public static void main(String[] args) throws Exception {
        // 默认使用类加载器
        ClassLoader loader = Demo05.class.getClassLoader();
        System.out.println(loader);  // o:jdk.internal.loader.ClassLoaders$AppClassLoader@3fee733d

        // 线程上下文类加载器
        ClassLoader loader2 = Thread.currentThread().getContextClassLoader();
        System.out.println(loader2);  // o:jdk.internal.loader.ClassLoaders$AppClassLoader@3fee733d

        // 线程上下文类加载器修改默认类加载器
        Thread.currentThread().setContextClassLoader(new FileSystemClassLoader(path));
        System.out.println(Thread.currentThread().getContextClassLoader());  // o:com.cty.FileSystemClassLoader@7291c18f

        Class c = Thread.currentThread().getContextClassLoader().loadClass("Hi");
        System.out.println(c);  // o:class Hi
        System.out.println(c.getClassLoader());  // o:myjava.p_classLoader.FileSystemClassLoader@1540e19d

    }
}
